|
|
|
|
|
|
|
First, you can set the AutoRedraw property of the object to true. In this case, Visual Basic allocates a piece of memory to represent the object's screen image. Whenever you perform a drawing command, Visual Basic executes the command in this memory. Visual Basic uses this memory to update the contents of the form or picture box whenever necessary. |
|
|
|
|
|
|
|
|
When the object is first displayed, Visual Basic uses this memory to draw the object. Later, if the object is covered up by another form and is then exposed, Visual Basic uses the memory to redisplay the newly exposed pieces of the object. |
|
|
|
|
|
|
|
|
When you set AutoRedraw to true, you can execute drawing commands at any time, and Visual Basic stores them in memory. Many programs execute drawing commands during a form's Load event procedure or the Load event procedure of the parent form if the object is a picture box. In this case, the program should set AutoRedraw to true so the drawing is displayed. |
|
|
|
|
|
|
|
|
If you do not set the AutoRedraw property to true, Visual Basic generates a Paint event any time part of the object needs to be updated. When Visual Basic first creates a drawing object, it generates a Paint event. Whenever another form covers part of a drawing object and then the object is uncovered, Visual Basic generates another Paint event. |
|
|
|
|
|
|
|
|
If you set a form or picture box's AutoRedraw property to false, you should redraw the picture inside the Paint event procedure. Then, whenever Visual Basic needs to redraw the picture, it executes your drawing commands. |
|
|
|
|
|
|
|
|
When you set the AutoRedraw property to false, Visual Basic does not need to allocate memory to store the object's screen image, so it saves some memory. On the other hand, it forces your program to execute the drawing commands every time the object receives a Paint event. If those commands take a long time, the picture redraw will occur slowly. In that case, you may want to set AutoRedraw to true so that Visual Basic can handle the details of redrawing the picture quickly. |
|
|
|
|
|
|
|
|
Example program Auto, shown in Figure 1.16, contains two picture boxes. The first has AutoRedraw set to true. The second draws in its Paint event handler. When you run the program, click on other forms so that they cover part of the picture boxes. Then click on the picture boxes to expose the covered parts. The picture box with AutoRedraw set to true redraws almost instantly. The picture box on the right takes much longer to redraw whenever it is exposed. |
|
|
|
|
|
|
|
|
Redrawing during Resize Events |
|
|
|
|
|
|
|
|
In many programs, redrawing forms when they are resized is important. When the user makes a form larger, more information can be displayed. When the user makes a form smaller, the images you want to display may no longer fit on the form. You can rearrange a form and redraw the pictures when the form receives a Resize event. |
|
|
|
|
|